package com.hae.shop.micoshopportalweb.controller.product;

import com.hae.shop.common.base.Response;
import com.hae.shop.common.constant.Constants;
import com.hae.shop.common.util.BeanConvertUtil;
import com.hae.shop.micoshopportalweb.controller.product.feign.OrderFeign;
import com.hae.shop.micoshopportalweb.controller.product.vo.OrderReqVo;
import com.hae.shop.order.model.OrderReqDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 订单
 */
@Controller
public class OrderController {

    @Autowired
    private OrderFeign orderFeign;

    @RequestMapping("/order")
    public String order() {
        return Constants.ORDER_FTL;
    }

    @RequestMapping("/phurase")
    public String phurase(@ModelAttribute("orderReqVo") OrderReqVo orderReqVo, Model model) {
        //调用订单服务创建订单
        try {
            Response<String> response = orderFeign.createOrder(BeanConvertUtil.EToT(orderReqVo, OrderReqDto.class));
            if (!response.isSuccess()) {
                model.addAttribute("error", response.getErrorMsg());
                return Constants.ORDER_FTL;
            }
            //跳转到支付页
            return Constants.REDIRECT_INDEX + "localhost:8091/pay?payoken=" + response.getResult();
        } catch (Exception e) {
            model.addAttribute("error", "系统内部异常!");
            return Constants.ERROR_FTL;
        }
    }
}
